﻿Imports Capa_de_Acceso_a_Datos

Public Class frmCargaNotasCredito

    Public PresentacionId As Integer
    Public Modo As String

    Private Sub btnExaminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExaminar.Click
        DialogoArchivo.ShowDialog()
    End Sub


    Private Sub DialogoArchivo_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles DialogoArchivo.FileOk
        txtPath.Text = DialogoArchivo.FileName
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        LimpiarCampos()
    End Sub

    Private Sub btnSeleccionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeleccionar.Click
        frmSeleccionarPresentacion.frmLlamada = Me.Name
        frmSeleccionarPresentacion.ShowDialog()
    End Sub

    Private Sub frmCargaNotasCredito_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frmNotasCredito.Visible = True
        Me.Dispose()
    End Sub

    Private Sub frmCargaNotasCredito_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CargarCombos()
        If Modo = "Modificacion" Then
            CargarNotaCreditoSeleccionada()
            Me.Text = "Modificar Nota de Crédito"
        ElseIf Modo = "Visualizacion" Then
            CargarNotaCreditoSeleccionada()
            Me.Text = "Visualizar Nota de Crédito"
            DeshabilitarCampos()
        Else
            'Modo Inserción
            PresentacionId = -1
        End If
    End Sub

    Private Sub DeshabilitarCampos()
        txtAno.ReadOnly = True
        txtCodigo.ReadOnly = True
        txtFarmacia.ReadOnly = True
        txtImporte.ReadOnly = True
        txtMes.ReadOnly = True
        txtOS.ReadOnly = True
        txtPath.ReadOnly = True
        dtpFecha.Enabled = False
        cboTipoEnvio.Enabled = False
        cboTipoNotaCredito.Enabled = False

        btnGuardar.Visible = False
        btnCancelar.Visible = False
        btnSeleccionar.Visible = False
        btnExaminar.Visible = False
        'gboDatosNC.Enabled = False
        'gboPresentación.Enabled = False
    End Sub


    Private Sub CargarNotaCreditoSeleccionada()
        txtAno.Text = frmNotasCredito.NotaCreditoSeleccionada.Ano
        txtMes.Text = frmNotasCredito.NotaCreditoSeleccionada.Mes
        txtFarmacia.Text = frmNotasCredito.NotaCreditoSeleccionada.Farmacia
        txtOS.Text = frmNotasCredito.NotaCreditoSeleccionada.ObraSocialXPlan
        txtCodigo.Text = frmNotasCredito.NotaCreditoSeleccionada.Codigo
        txtImporte.Text = frmNotasCredito.NotaCreditoSeleccionada.Importe
        txtPath.Text = frmNotasCredito.NotaCreditoSeleccionada.Path
        dtpFecha.Value = frmNotasCredito.NotaCreditoSeleccionada.Fecha
        cboTipoEnvio.SelectedValue = frmNotasCredito.NotaCreditoSeleccionada.TipoEnvioNCId
        cboTipoNotaCredito.SelectedValue = frmNotasCredito.NotaCreditoSeleccionada.TipoNCId
        PresentacionId = frmNotasCredito.NotaCreditoSeleccionada.PresentacionId
    End Sub

    Private Sub CargarCombos()

        'Combo TipoNotaCredito
        cboTipoNotaCredito.ValueMember = "TipoNotaCreditoId"
        cboTipoNotaCredito.DisplayMember = "TipoNotaCreditoNombre"
        cboTipoNotaCredito.DataSource = TipoNotaCreditoDAO.obtenerTiposNotasCredito(0)

        'Combo TipoEnvioNotaCredito
        cboTipoEnvio.ValueMember = "TipoEnvioNotaCreditoId"
        cboTipoEnvio.DisplayMember = "TipoEnvioNotaCreditoNombre"
        cboTipoEnvio.DataSource = TipoEnvioNotaCreditoDAO.obtenerTiposEnvioNotaCredito(0)

    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If CamposCompletos() Then
            If Modo = "Modificacion" Then
                NotaCreditoDAO.actualizarNotaCredito(frmNotasCredito.NotaCreditoSeleccionada.Id, PresentacionId, cboTipoNotaCredito.SelectedValue, cboTipoEnvio.SelectedValue, CDbl(txtImporte.Text), CInt(txtCodigo.Text), txtPath.Text, dtpFecha.Value)
            Else
                'Modo Inserción
                NotaCreditoDAO.insertarNotaCredito(PresentacionId, cboTipoNotaCredito.SelectedValue, cboTipoEnvio.SelectedValue, CDbl(txtImporte.Text), CInt(txtCodigo.Text), txtPath.Text, dtpFecha.Value)
                'TODO insertar movimiento cuenta corriente
                registrarMovimiento(PresentacionId, CDbl(txtImporte.Text))
                LimpiarCampos()
            End If

            MsgBox("La nota de crédito se ha guardado con éxito", MsgBoxStyle.Information)
        Else
            MsgBox("Por favor complete todos los campos", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub registrarMovimiento(ByVal PresentacionId As Integer, ByVal importeCredito As Double)
        Dim dt As DataTable = PresentacionDAO.obtenerPresentaciones(PresentacionId)
        Dim farmaciaId As Integer = dt.Rows(0)("FarmaciaId")
        Dim tipoNotaCredito As Integer = 6
        CuentaCorrienteDAO.CuentaCorrienteInsertarMovimiento(farmaciaId, tipoNotaCredito, 0, importeCredito, Today, "Nota de Crédito Nº " + txtCodigo.Text)
    End Sub

    Private Function CamposCompletos() As Boolean
        Dim completos As Boolean = True

        If txtAno.Text = "" Or txtCodigo.Text = "" Or txtImporte.Text = "" Then
            completos = False
        End If
        Return completos
    End Function

    Private Sub Salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Salir.Click
        frmNotasCredito.Visible = True
        Me.Dispose()
    End Sub

    Private Sub LimpiarCampos()
        txtAno.Text = ""
        txtMes.Text = ""
        txtOS.Text = ""
        txtFarmacia.Text = ""
        txtPath.Text = ""
        txtCodigo.Text = ""
        txtImporte.Text = ""
    End Sub

End Class